package com.theaf.basemodule.service.impl;

import com.theaf.basemodule.common.api.LogService;
import com.theaf.basemodule.common.condition.LogCondition;
import com.theaf.basemodule.common.dto.log.LogDTO;
import com.theaf.basemodule.common.dto.log.LogHisDTO;
import com.theaf.basemodule.common.model.SysLog;
import com.theaf.basemodule.common.model.SysLogSub;
import com.theaf.basemodule.common.model.SysResource;
import com.theaf.basemodule.dao.ResourceDAO;
import com.theaf.basemodule.dao.SysLogDAO;
import com.theaf.core.annotation.MqConfig;
import com.theaf.core.repo.kit.SimplePageBuilder;
import com.theaf.core.repo.kit.SimpleSortBuilder;
import com.theaf.core.repo.specification.SqlBuilder;
import com.theaf.core.repo.specification.SqlOperator;
import com.theaf.core.repo.specification.SqlValue;
import com.theaf.core.rmq.MessageProcess;
import com.theaf.core.rmq.common.MqReturn;
import com.theaf.core.service.Log;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author:songwenke
 * @Description:日志服务实现
 * @Date:create in 16:28 2018/2/11
 * @Modified By:
 **/
@Service
@MqConfig(topic="topic_log")
public class LogServiceImpl implements LogService,MessageProcess<Log> {
    @Autowired
    private SysLogDAO logDAO;
    @Autowired
    private ResourceDAO resourceDAO;
    @Autowired
    private Mapper mapper;

    @Override
    public Page<LogDTO> findByCondition(LogCondition condition) {
        Map orderMap = new HashMap();
        orderMap.putIfAbsent("reqTime",false);//时间倒序
        return logDAO.findAll(SqlBuilder.start("resource.sysTag",condition.getAppCode())//系统条件
                .and("resource.catalog", SqlOperator.OPER.LIKE, SqlValue.parse(condition.getResourceName()))//模块目录资源条件
                .and("opeName", SqlOperator.OPER.LIKE,SqlValue.parse(condition.getMan()))//操作人条件
                .and("reqTime", SqlOperator.OPER.GE,condition.getStartTime())//开始时间
                .and("reqTime", SqlOperator.OPER.LEA,condition.getEndTime()).gen(),//结束时间
                SimplePageBuilder.generate(condition.getPage(),condition.getSize(),//分页
                        SimpleSortBuilder.generateSort(orderMap))).map(n->{
                            SysLog log = (SysLog)n;
                            LogDTO logDTO = mapper.map(n,LogDTO.class);
                            logDTO.setResourceName(log.getResource().getName());
                            //logDTO.setSysName(DictionaryHelper.getChlidValue("sys_type",log.getResource().getSysTag()));
                            logDTO.setDatas(log.getSysLogSubs().stream().map(t->t.getContent()).collect(Collectors.toList()));
                            return logDTO;
                        });
    }

    @Override
    public List<LogHisDTO> findHisLog(Long dataId) {
        return (List<LogHisDTO>) logDAO.findAll(SqlBuilder.start("sysLogSubs.dataId",dataId).gen()).parallelStream().map(n->{
            LogHisDTO dto = new LogHisDTO();
            SysLog log = (SysLog)n;
            dto.setAction(log.getAction());
            dto.setData(log.getSysLogSubs().parallelStream().filter(
                    y->y.getDataId().equals(dataId)).map(t->t.getContent()).findFirst().get());
            dto.setOpeIp(log.getOpeIp());
            dto.setOpeName(log.getOpeName());
            dto.setOpeTer(log.getOpeTer());
            dto.setReqTime(log.getReqTime());
            dto.setResourceName(log.getResource().getName());
            //dto.setSysName(DictionaryHelper.getChlidValue("sys_type",log.getResource().getSysTag()));
            return dto;
        }).collect(Collectors.toList());
    }

    @Override
    public MqReturn process(Log log, int excuteNumber, String id) {
        SysLog sysLog = mapper.map(log,SysLog.class);
        SysResource sysResource = resourceDAO.findOne(SqlBuilder.start("sysTag",log.getAppCode())
                .and("url",SqlValue.parse(log.getUrl())).gen());
        sysLog.setResource(sysResource);
        if(log.getDatas()!=null&&log.getDatas().size()>0) {
            List datas = new ArrayList();
            log.getDatas().forEach((k,v)->{
                SysLogSub sysLogSub = new SysLogSub();
                sysLogSub.setContent(v);
                sysLogSub.setDataId(k);
            });
            sysLog.setSysLogSubs(datas);
        }
        logDAO.save(sysLog);
        return MqReturn.success(null);
    }
}
